package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingDTO;
import com.itheima.pojo.OrderSettingVO;
import com.itheima.sevice.OrderSettingService;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    /**
     * 模板下载
     * @param response
     * @return
     */
    @GetMapping("/download")
    public String download(HttpServletResponse response, String filename){
        orderSettingService.download(response, filename);

        return "文件下载";
    }

    /**
     * 根据日期修改可预约人数
     * @param orderSettingDTO
     * @return
     */
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSettingDTO orderSettingDTO){
        Integer recordCount = orderSettingService.editNumberByOrderDate(orderSettingDTO);

        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS, recordCount);
    }

    /**
     * 根据年月查询预约设置
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){
        List<OrderSettingVO> orderSettingVOList = orderSettingService.getOrderSettingByMonth(month);

        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS, orderSettingVOList);
    }

    /**
     * 上传excel文件
     * @param excelFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) throws Exception {
        Integer recordCount = orderSettingService.upload(excelFile);

        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS, recordCount);
    }
}
